<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>楼层滚动</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul {
            list-style: none;
        }
        
        .bigbox {}
        
        .bigbox .box {
            width: 600px;
            height: 400px;
            background-color: #aaa;
            text-align: center;
            color: #0ff;
            line-height: 40px;
            margin-bottom: 40px;
        }
        
        .bigbox .box h2 {}
        
        .box-nav {
            position: fixed;
            right: 20px;
            bottom: 100px;
            color: #0ff;
        }
        
        .box-nav li {
            width: 100px;
            text-align: center;
            margin-bottom: 20px;
            background-color: #aaa;
        }
        
        .box-nav li:hover {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="bigbox">
        <div class="box">
            <span>一楼</span>
        </div>
        <div class="box">
            <span>二楼</撒泡尿>
        </div>
        <div class="box">
           <span>三楼</span>
        </div>
        <div class="box">
            <span>四楼</span>
        </div>
        <div class="box">
            <span>五楼</span>
        </div>
        <div class="box-nav">
            <ul>
                <li>一楼</li>
                <li>二楼</li>
                <li>三楼</li>
                <li>四楼</li>
                <li>五楼</li>
            </ul>
        </div>
    </div>

    <script>
        let lis = document.querySelectorAll(".box-nav li")
        let spans = document.querySelectorAll(".box span")
        timer = null
        lis.forEach(function(item, index) {
            item.addEventListener("click", function() {
                // 当前滚动距离
                let current = document.documentElement.scrollTop
                    // 目标楼层需要滚动的距离
                let target = spans[index].offsetTop
                let timer = setInterval(function() {
                    // 目标楼层需要滚动的距离 大于 当前滚动距离
                    if (target > current) {
                        current += 20
                        document.documentElement.scrollTop = current
                    } else {
                        current -= 20
                        document.documentElement.scrollTop = current
                    }
                    if (Math.abs(current - target) <= 20) {
                        clearInterval(timer)
                        timer = null
                        current = target
                        document.documentElement.scrollTop = target
                    }
                }, 20)

            })

        })
    </script>
</body>

</html>